var express = require('express');
var router = express.Router();
var userCtrl = require('../controller/userCtrl');
var exec_python = require('../system/exec_python');

// 校验格式
var checkUser = function(req, res, next) {

  if (!!req.query.name && !!req.query.age) {
    next();
  };
};

// 执行python脚本
var execPython = function(req, res, next) {
  console.log('execPython1');
  exec_python.execPythonScript('py_test.py', ['hello', 'world'], 'python2.7', function(ret) {
    console.log(ret);
    next();
  });
};

// 插入数据库
var addUser = function(req, res, next) {
  console.log('insert db');
  userCtrl.addUser(req, res, function(ret) {
    console.log('insert db success', ret);
    res.json(ret);
  });
};

// 查询数据库
var findUser = function(req, res) {
  console.log('query db');
  userCtrl.findUser(req, res, function(ret) {
    console.log('find user success', ret);
    res.json(ret);
  })
}

// 插入用户
router.get('/add', [checkUser, addUser]);
router.get('/find', [execPython, findUser]);
module.exports = router;
